package drds.binlog.store.model;

import com.google.protobuf.ByteString;
import drds.binlog.common.position.LogIdentity;
import drds.binlog.protocol.entry_protocol_proto.Entry;
import drds.binlog.protocol.entry_protocol_proto.EntryType;
import drds.binlog.protocol.entry_protocol_proto.EventType;
import drds.binlog.protocol.entry_protocol_proto.Pair;

import java.io.Serializable;
import java.util.List;

/**
 * store存储数据对象
 */
public class Event implements Serializable
{

    private static final long serialVersionUID = 1333330351758762739L;

    private LogIdentity logIdentity;                            // 记录数据产生的来源
    private ByteString rawEntry;

    private long executeDateTimeLong;
    private EntryType entryType;
    private String journalName;
    private long position;
    private long serverId;
    private EventType eventType;
    private String gtid;
    private long rawLength;
    private int rowsCount;

    public Event()
    {
    }

    public Event(LogIdentity logIdentity, Entry entry)
    {
        this.logIdentity = logIdentity;
        this.entryType = entry.getEntryType();
        this.executeDateTimeLong = entry.getHeader().getExecuteTime();
        this.journalName = entry.getHeader().getLogfileName();
        this.position = entry.getHeader().getLogfileOffset();
        this.serverId = entry.getHeader().getServerId();
        this.gtid = entry.getHeader().getGtid();
        this.eventType = entry.getHeader().getEventType();
        // build raw
        this.rawEntry = entry.toByteString();
        this.rawLength = rawEntry.size();
        if (entryType == EntryType.ROWDATA)
        {
            List<Pair> props = entry.getHeader().getPropsList();
            if (props != null)
            {
                for (Pair p : props)
                {
                    if ("rowsCount".equals(p.getKey()))
                    {
                        rowsCount = Integer.parseInt(p.getValue());
                        break;
                    }
                }
            }
        }
    }

    public LogIdentity getLogIdentity()
    {
        return logIdentity;
    }

    public void setLogIdentity(LogIdentity logIdentity)
    {
        this.logIdentity = logIdentity;
    }

    public ByteString getRawEntry()
    {
        return rawEntry;
    }

    public void setRawEntry(ByteString rawEntry)
    {
        this.rawEntry = rawEntry;
    }

    public long getExecuteDateTimeLong()
    {
        return executeDateTimeLong;
    }

    public void setExecuteDateTimeLong(long executeDateTimeLong)
    {
        this.executeDateTimeLong = executeDateTimeLong;
    }

    public EntryType getEntryType()
    {
        return entryType;
    }

    public void setEntryType(EntryType entryType)
    {
        this.entryType = entryType;
    }

    public String getJournalName()
    {
        return journalName;
    }

    public void setJournalName(String journalName)
    {
        this.journalName = journalName;
    }

    public long getPosition()
    {
        return position;
    }

    public void setPosition(long position)
    {
        this.position = position;
    }

    public long getServerId()
    {
        return serverId;
    }

    public void setServerId(long serverId)
    {
        this.serverId = serverId;
    }

    public String getGtid()
    {
        return gtid;
    }

    public void setGtid(String gtid)
    {
        this.gtid = gtid;
    }

    public long getRawLength()
    {
        return rawLength;
    }

    public void setRawLength(long rawLength)
    {
        this.rawLength = rawLength;
    }

    public EventType getEventType()
    {
        return eventType;
    }

    public void setEventType(EventType eventType)
    {
        this.eventType = eventType;
    }

    public int getRowsCount()
    {
        return rowsCount;
    }

    public void setRowsCount(int rowsCount)
    {
        this.rowsCount = rowsCount;
    }


}
